package com.yudaxian.bi.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.wf.captcha.GifCaptcha;
import com.yudaxian.bi.common.BaseResponse;
import com.yudaxian.bi.common.Const;
import com.yudaxian.bi.common.ResultUtils;
import com.yudaxian.bi.model.dto.user.UserLoginRequest;
import com.yudaxian.bi.model.dto.user.UserRegisterRequest;
import com.yudaxian.bi.model.dto.user.UserUpdatePasswordRequest;
import com.yudaxian.bi.model.dto.user.UserUpdateRequest;
import com.yudaxian.bi.model.vo.UserCheckCodeVO;
import com.yudaxian.bi.model.vo.UserLoginVO;
import com.yudaxian.bi.service.UserService;
import com.yudaxian.bi.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author: yudaxian
 * @Description: 用户控制器
 * @DateTime: 2024/9/16 11:27
 **/
@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Resource
    private UserService userService;

    @Resource
    private RedisUtil redisUtil;

    @PostMapping("/register")
    @ApiOperation(value = "用户注册", notes = "用户注册接口")
    public BaseResponse register(@RequestBody UserRegisterRequest userRegisterRequest) {
        this.userService.register(userRegisterRequest);
        return ResultUtils.success(null);
    }

    @PostMapping("/getUserInfo")
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息接口")
    public BaseResponse getUserInfo() {
        return ResultUtils.success(this.userService.getById(StpUtil.getLoginId().toString()));
    }

    @PostMapping("/updateUserInfo")
    @ApiOperation(value = "更新用户信息", notes = "更新用户信息接口")
    public BaseResponse updateUserInfo(@RequestBody UserUpdateRequest UserUpdateRequest) {
        this.userService.updateUserInfo(UserUpdateRequest);
        return ResultUtils.success(null);
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "用户登录接口")
    public BaseResponse login(@RequestBody UserLoginRequest userLoginRequest) {
        UserLoginVO vo = this.userService.login(userLoginRequest);
        return ResultUtils.success(vo);
    }

    @PostMapping("/logout")
    @ApiOperation(value = "用户登出", notes = "用户登出接口")
    public BaseResponse logout() {
        StpUtil.logout();
        return ResultUtils.success(null);
    }

    @PostMapping("/updatePassword")
    @ApiOperation(value = "更新密码", notes = "更新密码接口")
    public BaseResponse updatePassword(@RequestBody UserUpdatePasswordRequest updatePassword) {
        this.userService.updatePassword(updatePassword);
        return ResultUtils.success(null);
    }

    @PostMapping("/checkCode")
    @ApiOperation(value = "验证码", notes = "验证码接口")
    public BaseResponse checkCode() {
        GifCaptcha captcha = new GifCaptcha(130, 48);
        String code = captcha.text();
        String base64 = captcha.toBase64();
        String checkCodeKey = UUID.randomUUID().toString();
        UserCheckCodeVO vo = new UserCheckCodeVO();
        vo.setCheckCode(code);
        vo.setCheckCodeImg(base64);
        vo.setCheckCodeKey(checkCodeKey);
        log.info("checkCodeKey:{}, code:{}", checkCodeKey, code);

       redisUtil.set(Const.CHECKCODE_KEY + checkCodeKey, code, 60);

        return ResultUtils.success(vo);
    }

}
